package com.yhkj.data.config;

import com.yhkj.data.datasource.RoutingDataSource;
import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 * @author <a href="mailto:idler41@163.con">linfuxin</a> created on 2023-02-24 14:59:07
 */
@SpringBootApplication(scanBasePackages = "com.yhkj.data", exclude = {DataSourceAutoConfiguration.class})
@EnableScheduling
public class AppConfig {
    @Bean
    public RoutingDataSource routingDataSource() {
        return new RoutingDataSource();
    }

    @Bean
    public Validator validator(AutowireCapableBeanFactory springFactory) {
        try (ValidatorFactory factory = Validation.byProvider(HibernateValidator.class)
                .configure()
                .failFast(true)
                .constraintValidatorFactory(new SpringConstraintValidatorFactory(springFactory)).buildValidatorFactory()) {
            return factory.getValidator();
        }
    }
}
